//Jen Anderson
//CS 362
//Assignment 3
//unittest1.c

/*----------------------------------------------------------------------
  Unit test for playCard()
  Code borrowed heavily from testUpdateCoins() example given on 
  	blackboard
  Tests whether playCard()
	Test 1: detects a non-zero phase
	Test 2: detects 0 actions left	
	Test 3: returns -1 if the card is an action card
	Test 4: applies the card to the correct hand
	Test 5: reduces the number of actions
-----------------------------------------------------------------------*/

#include <assert.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//set NOISY_TEST to 0 to remove print output
#define NOISY_TEST 1

void testPlayCard()
{
	//Initialize the game
        int k[10] = {adventurer, council_room, feast, gardens, mine,remodel, smithy, village, baron, great_hall};
        struct gameState *game = newGame();
        initializeGame(3, k, 10, game);
        struct gameState *testGameState = malloc(sizeof(struct gameState));
        memcpy(testGameState, game, sizeof(struct gameState));
        
	/**************
	* Test 1
	**************/
	//Test whether playCard() successfully checks whether there is an incorrect phase
	//Phase should be set to 0.  If not, playCard() returns -1
        testGameState->phase = 1;
	assert(playCard(1, 0, 0, 0, testGameState) == -1);
	#if (NOISY_TEST == 1)
                printf("playCard() successfully detected the phase wasn't set to 0.\n");
        #endif
          
	/*************
	* Test 2
	*************/
	//Reset the phase to 0
	//This section tests whether playCard() successfully returns -1 if the player has no actions left 
        testGameState->phase = 0;
        testGameState->numActions = 0;
	assert(playCard(1, 0, 0, 0, testGameState) == -1);
        #if (NOISY_TEST == 1)
                printf("playCard() successfully detected there were no actions left.\n");
        #endif
        
	/************
	* Test 3
	************/
	//Reset the number of actions to a non-zero number
	//Tests whether the card is not an action card
        testGameState->numActions = 1;
        testGameState->hand[testGameState->whoseTurn][1] = adventurer-1;
        assert(playCard(1, 0, 0, 0, testGameState) == -1);
        #if (NOISY_TEST == 1)        
		printf("playCard() successfully detected the card is not an action card\n");
        #endif
                
	/***********
	* Test 4
	***********/
	//Reset the card to an action card
	//Make sure the function applies the card to the right player
        testGameState->hand[testGameState->whoseTurn][1] = smithy;
        playCard(1, 0, 0, 0, testGameState);
	assert(testGameState->handCount[testGameState->whoseTurn] == game->handCount[testGameState->whoseTurn]+2);
        #if (NOISY_TEST == 1)
	        printf("playCard() applied the card effect\n");
        #endif
        
	/**********
	* Test 5
	**********/
	//Make sure the number of actions is reduced by 1
	//playCard was called in Test 4
	assert(testGameState->numActions == game->numActions-1);
        #if (NOISY_TEST == 1)
	        printf("playCard() successfully reduced the number of actions.\n");
        #endif
}

int main(int argc, char *argv[])
{
    testPlayCard();
    return 0;
}
